<template>
  <div class="router-tutorial">
    <div class="tutorial-content">
      <h2>编程式导航</h2>
      <p>除了使用 &lt;router-link&gt; 创建导航链接，我们还可以通过编程方式导航到不同的路由。</p>
      
      <h3>使用 router.push</h3>
      <p>在 Vue Router 中，最常用的编程式导航方法是 <code>router.push</code>。这个方法会向历史记录栈添加一个新记录：</p>
      <pre>
// 字符串路径
router.push('/home')

// 带有路径的对象
router.push({ path: '/home' })

// 命名的路由，并加上参数
router.push({ name: 'user', params: { userId: '123' } })

// 带查询参数
router.push({ path: '/search', query: { q: 'vue' } })</pre>
      
      <h3>使用 router.replace</h3>
      <p><code>router.replace</code> 与 <code>router.push</code> 类似，但它不会向历史添加新记录，而是替换当前记录：</p>
      <pre>
router.replace({ path: '/home' })</pre>
      
      <h3>使用 router.go</h3>
      <p>这个方法接收一个整数作为参数，表示在历史堆栈中前进或后退多少步：</p>
      <pre>
// 前进一步，等同于 history.forward()
router.go(1)

// 后退一步，等同于 history.back()
router.go(-1)

// 前进 3 步
router.go(3)</pre>
    </div>
    
    <div class="code-editor">
      <div class="file-tabs">
        <button @click="currentFile = 'App'">App.vue</button>
        <button @click="currentFile = 'Navigation'">Navigation.vue</button>
      </div>
      
      <div class="code-preview" v-if="currentFile === 'App'">
        <pre><code>{{ appCode }}</code></pre>
      </div>
      
      <div class="code-preview" v-if="currentFile === 'Navigation'">
        <pre><code>{{ navigationCode }}</code></pre>
      </div>
      
      <div class="result-preview">
        <h4>预览效果：</h4>
        <div class="preview-container">
          <div class="app-container">
            <h2>编程式导航示例</h2>
            <div class="buttons">
              <button @click="navigateTo('home')">首页</button>
              <button @click="navigateTo('about')">关于</button>
              <button @click="navigateTo('user', { id: 123 })">用户页面</button>
              <button @click="goBack">后退</button>
              <button @click="goForward">前进</button>
            </div>
            <div class="current-route">
              <p>当前路由: <strong>{{ currentRoute }}</strong></p>
              <p v-if="currentParam">参数: <strong>{{ currentParam }}</strong></p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';

const currentFile = ref('App');
const currentRoute = ref('home');
const currentParam = ref(null);

function getAppCode() {
  return `<template>
  <div id="app">
    <h2>编程式导航示例</h2>
    <div class="buttons">
      <button @click="navigateToHome">首页</button>
      <button @click="navigateToAbout">关于</button>
      <button @click="navigateToUser">用户页面</button>
      <button @click="goBack">后退</button>
      <button @click="goForward">前进</button>
    </div>
    <router-view></router-view>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router';

const router = useRouter();

function navigateToHome() {
  router.push('/');
}

function navigateToAbout() {
  router.push({ path: '/about' });
}

function navigateToUser() {
  router.push({ 
    name: 'user', 
    params: { id: 123 }
  });
}

function goBack() {
  router.go(-1);
}

function goForward() {
  router.go(1);
}
<\/script>

<style>
.buttons {
  margin: 20px 0;
}

button {
  margin-right: 10px;
  padding: 5px 10px;
  background-color: #42b883;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

button:hover {
  background-color: #3eaf7c;
}
</style>`;
}

function getNavigationCode() {
  return `<template>
  <div class="navigation">
    <h3>Navigation 组件</h3>
    <p>这个组件展示了如何使用编程式导航功能</p>
    
    <div class="demo">
      <button @click="navigateWithReplace">
        使用 replace 导航 (不记录历史)
      </button>
      
      <button @click="navigateWithQuery">
        带查询参数的导航
      </button>
    </div>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router';

const router = useRouter();

function navigateWithReplace() {
  // 使用 replace 导航到首页，不会添加新的历史记录
  router.replace('/');
}

function navigateWithQuery() {
  // 导航到带有查询参数的页面
  router.push({
    path: '/search',
    query: {
      q: 'vue router',
      category: 'documentation'
    }
  });
}
<\/script>

<style scoped>
.navigation {
  padding: 20px;
  border: 1px solid #ddd;
  border-radius: 8px;
}

.demo {
  margin-top: 15px;
}

button {
  display: block;
  margin: 10px 0;
  padding: 8px 15px;
}
</style>`;
}

// 在这里使用方法调用而不是直接在模板中嵌套字符串，避免嵌套模板字符串问题
const appCode = getAppCode();
const navigationCode = getNavigationCode();

function navigateTo(route, params = null) {
  currentRoute.value = route;
  currentParam.value = params ? params.id : null;
}

function goBack() {
  // 模拟后退功能
  alert('后退到上一页');
}

function goForward() {
  // 模拟前进功能
  alert('前进到下一页');
}
</script>

<style scoped>
.router-tutorial {
  display: flex;
  height: 100%;
}

.tutorial-content {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  border-right: 1px solid #ddd;
}

.code-editor {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.file-tabs {
  display: flex;
  background-color: #f0f0f0;
  border-bottom: 1px solid #ddd;
}

.file-tabs button {
  padding: 8px 16px;
  border: none;
  background: none;
  cursor: pointer;
}

.file-tabs button:hover {
  background-color: #e0e0e0;
}

.code-preview {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
  background-color: #f9f9f9;
}

pre {
  margin: 0;
  white-space: pre-wrap;
  font-family: monospace;
}

.result-preview {
  height: 300px;
  border-top: 1px solid #ddd;
  padding: 10px;
  overflow-y: auto;
}

.preview-container {
  border: 1px solid #ddd;
  border-radius: 5px;
  padding: 15px;
  background-color: white;
}

.app-container {
  text-align: center;
}

.buttons {
  margin: 20px 0;
}

button {
  margin-right: 5px;
  padding: 5px 10px;
  background-color: #42b883;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

button:hover {
  background-color: #3eaf7c;
}

.current-route {
  margin-top: 20px;
  padding: 10px;
  background-color: #f5f5f5;
  border-radius: 5px;
  text-align: left;
}
</style> 